﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace Ji.DataHelper
{
    public static class EnumHelper
    {

        /// <summary> 获取枚举的声明名称 </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string GetName(this Enum data)
        {
            return Enum.GetName(data.GetType(), data);
        }

        /// <summary> 获取枚举的描述属性 </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Dictionary<T, string> GetAllEnumDescriptions<T>(this T data) where T : struct
        {
            Dictionary<T, string> dirs = new Dictionary<T, string>();
            var type = data.GetType();
            foreach (var i in type.GetFields())
            {
                if (i.FieldType.Equals(type))
                {
                    var enumdata = (T)Enum.Parse(type, i.Name);
                    var attribute = enumdata.GetAttribute<T, DescriptionAttribute>(i.Name);
                    if (attribute != null)
                    {
                        dirs.Add(enumdata, attribute.Description);
                    }
                }
            }
            return dirs;
        }

    }
}
